home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 501-525 / disk_525 / siod / siod.doc < prev    next >
Text File  |  1992-05-06  |  18KB  |  522 lines

  1.  *                        COPYRIGHT (c) 1989 BY                             *
  2.  *        PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS.       *
  3.  *        See the source file SLIB.C for more information.                  *
  4.  
  5. Documentation for Release 2.4 27-APR-90, George Carrette
  6.  
  7. [Release Notes:]
  8.  
  9. 1.4 This release is functionally the same as release 1.3 but has been
  10. remodularized in response to people who have been encorporating SIOD
  11. as an interpreted extension language in other systems.
  12.  
  13. 1.5 Added the -g flag to enable mark-and-sweep garbage collection.
  14.     The default is stop-and-copy.
  15.  
  16. 2.0 Set_Repl_Hooks, catch & throw. 
  17.  
  18. 2.1 Additions to SIOD.SCM: Backquote, cond.
  19.  
  20. 2.2 User Type extension. Read-Macros. (From C-programmer level).
  21.  
  22. 2.3 save-forms. load with argument t, comment character, faster intern.
  23.     -o flag gives obarray size. default 100.
  24.  
  25. 2.4 speed up arithmetic and the evaluator. fixes to siod.scm. no_interrupt
  26.     around calls to C I/O. gen_readr.
  27.  
  28. gjc@paradigm.com
  29. George Carrette
  30.  
  31.    
  32. Paradigm Associates Inc          Phone: 617-492-6079
  33. 29 Putnam Ave, Suite 6
  34. Cambridge, MA 02138
  35.  
  36. [Files:]
  37.  
  38.  siod.h    Declarations 
  39.  slib.c    scheme library.
  40.  siod.c    a main program.
  41.  siod.scm  Some scheme code
  42.  pratt.scm A pratt-parser in scheme.
  43.  
  44.  
  45. [Motivation:]
  46.  
  47. The most obvious thing one should notice is that this lisp implementation 
  48. is extremely small. For example, the resulting binary executable file 
  49. on a VAX/VMS system with /notraceback/nodebug is 17 kilo-bytes.
  50.  
  51. Small enough to understand, the source file slib.c is 30 kilo-bytes.
  52.  
  53. Small enough to include in the smallest applications which require
  54. command interpreters or extension languages.
  55.  
  56. We also want to be able to run code from the book "Structure and
  57. Interpretation of Computer Programs." 
  58.  
  59. Techniques used will be familiar to most lisp implementors.  Having
  60. objects be all the same size, and having only two statically allocated
  61. spaces simplifies and speeds up both consing and gc considerably.  the
  62. MSUBR hack allows for a modular implementation of tail recursion,     
  63. an extension of the FSUBR that is, as far as I know, original.
  64. The optional mark and sweep garbage collector may be selected at runtime.
  65.  
  66. Error handling is rather crude. A topic taken with machine fault,
  67. exception handling, tracing, debugging, and state recovery which we
  68. could cover in detail, but is clearly beyond the scope of this
  69. implementation. Suffice it to say that if you have a good symbolic
  70. debugger you can set a break point at "err" and observe in detail all
  71. the arguments and local variables of the procedures in question, since
  72. there is no casting of data types. For example, if X is an offending
  73. or interesting object then examining X->type will give you the type,
  74. and X->storage_as.cons will show the car and the cdr.
  75.  
  76. [Invocation:]
  77.  
  78. siod [-hXXXXX] [-iXXXXX] [-gX] [-oXXXXX]
  79.  -h where XXXXX is an integer, to specify the heap size, in obj cells,
  80.  -i where XXXXX is a filename to load before going into the repl loop.
  81.  -g where X = 1 for stop-and-copy GC, X = 0 for mark-and-sweep.
  82.  -o where XXXXX is the size of the symbol hash table to use, default 100.
  83.  
  84.   Example:
  85.    siod -isiod.scm -h100000
  86.  
  87. [Garbage Collection:]
  88.  
  89. There are two storage management techniques which may be chosen at runtime
  90. by specifying the -g argument flag. 
  91.  
  92.  -g1 (the default) is stop-and-copy. This is the simplest and most
  93.      portable implementation. GC is only done at toplevel.
  94.  -g0 is mark-and-sweep. GC is done at any time, required or requested.
  95.      However, the implementation is not as portable.
  96.  
  97. Discussion of stop-and-copy follows:
  98.  
  99. As one can see from the source, garbage collection is really quite an easy
  100. thing. The procedure gc_relocate is about 25 lines of code, and
  101. scan_newspace is about 15.
  102.  
  103. The real tricks in handling garbage collection are (in a copying gc):
  104.  (1) keeping track of locations containing objects
  105.  (2) parsing the heap (in the space scanning)
  106.  
  107. The procedure gc_protect is called once (e.g. at startup) on each
  108. "global" location which will contain a lisp object.
  109.  
  110. That leaves the stack. Now, if we had chosen not to use the argument
  111. and return-value passing mechanism provided by the C-language
  112. implementation, (also known as the "machine stack" and "machine
  113. procedure calling mechanism) this lisp would be larger, slower, and
  114. rather more difficult to read and understand. Furthermore it would be
  115. considerably more painful to *add* functionality in the way of SUBR's
  116. to the implementation.
  117.  
  118. Aside from writing a very machine and compiler specific assembling language
  119. routine for each C-language implementation, embodying assumptions about
  120. the placement choices for arguments and local values, etc, we
  121. are left with the following limitation: "YOU CAN GC ONLY AT TOP-LEVEL"
  122.  
  123. However, this fits in perfectly with the programming style imposed in
  124. many user interface implementations including the MIT X-Windows Toolkit.
  125. In the X Toolkit, a callback or work procedure is not supposed to spend
  126. much time implementing the action. Therefore it cannot have allocated
  127. much storage, and the callback trampoline mechanism can post a work
  128. procedure to call the garbage collector when needed.
  129.  
  130. Our simple object format makes parsing the heap rather trivial.
  131. In more complex situations one ends up requiring object headers or markers
  132. of some kind to keep track of the actual storage lengths of objects
  133. and what components of objects are lisp pointers.
  134.  
  135.  
  136. Discussion of mark-and-sweep follows:
  137.  
  138. In a mark-and-sweep GC the objects are not relocated. Instead
  139. one only has to LOOK at objects which are referenced by the argument
  140. frames and local variables of the underlying (in this case C-coded)
  141. implementation procedures. If a pointer "LOOKS" like it is a valid
  142. lisp object (see the procedure mark_locations_array) then it may be marked,
  143. and the objects it points to may be marked, as being in-use storage which
  144. is not linked into the freelist in the gc_sweep phase.
  145.  
  146. Another advantage of the mark_and_sweep storage management technique is
  147. that only one heap is required.
  148.  
  149. This main disadvantages are:
  150. (1) start-up cost to initially link freelist.
  151.     (can be avoided by more general but slower NEWCELL code).
  152. (2) does not COMPACT or LOCALIZE the use of storage. This is POOR engineering
  153.     practice in a virtual memory environment.
  154. (2) the entire heap must be looked at, not just the parts with useful storage.
  155.  
  156. In general, mark-and-sweep is slower in that it has to look at more
  157. memory locations for a given heap size, however the heap size can
  158. be smaller for a given problem being solved. More complex analysis
  159. is required when READ-ONLY, STATIC, storage spaces are considered.
  160. Additionally the most sophisticated stop-and-copy storage management
  161. techniques take into account considerations of object usage temporality.
  162.  
  163. [Compilation:]
  164.  
  165. The code has been compiled and run by the author on Sun III and IV,
  166. Encore Multimax, 4.3BSD VAX, VAX/VMS, AMIGA 500 using the Lattice C
  167. compiler, MacIntosh using THINK C version 4.0
  168.  
  169. On all unix machines use (with floating-point flags as needed)
  170.   
  171.   %cc -O -c siod.c
  172.   %cc -O -c slib.c
  173.   %cc -o siod siod.o slib.o
  174.  
  175. on VAX/VMS:
  176.  
  177.   $ cc siod
  178.   $ cc slib
  179.   $ link siod,slib,sys$input:/opt
  180.   sys$library:vaxcrtl/share
  181.   $ siod == "$" + F$ENV("DEFAULT") + "SIOD"
  182.  
  183. on AMIGA 500, ignore warning messages about return value mismatches,
  184.   %lc siod.c
  185.   %lc slib.c
  186.   %blink lib:c.o,siod.o,slib.o to siod lib lib:lcm.lib,lib:lc.lib,lib:amiga.lib
  187.  
  188. in THINK C.
  189.   The siod project must include siod.c,slib.c,siod_m.c, Mactraps, ansi, unix.
  190.  
  191.  
  192. [System:]
  193.  
  194. The interrupts called SIGINT and SIGFPE by the C runtime system are
  195. handled by invoking the lisp error procedure. SIGINT is usually caused
  196. by the CONTROL-C character and SIGFPE by floating point overflow or underflow.
  197.  
  198. [Syntax:]
  199.  
  200. The only special characters are the parenthesis and single quote.
  201. Everything else, besides whitespace of course, will make up a regular token.
  202. These tokens are either symbols or numbers depending on what they look like.
  203. Dotted-list notation is not supported on input, only on output.
  204.  
  205. [Special forms:]
  206.  
  207. The CAR of a list is evaluated first, if the value is a SUBR of type 9 or 10
  208. then it is a special form.
  209.  
  210. (define symbol value) is presently like (set! symbol value).
  211.  
  212. (define (f . arglist) . body) ==> (define f (lambda arglist . body))
  213.  
  214. (lambda arglist . body) Returns a closure.
  215.  
  216. (if pred val1 val2) If pred evaluates to () then val2 is evaluated else val1.
  217.  
  218. (begin . body) Each form in body is evaluated with the result of the last
  219. returned.
  220.  
  221. (set! symbol value) Evaluates value and sets the local or global value of
  222. the symbol.
  223.  
  224. (or x1 x2 x3 ...) Returns the first Xn such that Xn evaluated non-().
  225.  
  226. (and x1 x2 x3 ...) Keeps evaluating Xj until one returns (), or Xn.
  227.  
  228. (quote form). Input syntax 'form, returns form without evaluation.
  229.  
  230. (let pairlist . body) Each element in pairlist is (variable value).
  231. Evaluates each value then sets of new bindings for each of the variables,
  232. then evaluates the body like the body of a progn. This is actually
  233. implemented as a macro turning into a let-internal form.
  234.  
  235. (the-environment) Returns the current lexical environment.
  236.  
  237. [Macro Special forms:]
  238.  
  239. If the CAR of a list evaluates to a symbol then the value of that symbol
  240. is called on a single argument, the original form. The result of this
  241. application is a new form which is recursively evaluated.
  242.  
  243. [Built-In functions:]
  244.  
  245. These are all SUBR's of type 4,5,6,7, taking from 0 to 3 arguments
  246. with extra arguments ignored, (not even evaluated!) and arguments not
  247. given defaulting to (). SUBR's of type 8 are lexprs, receiving a list
  248. of arguments. Order of evaluation of arguments will depend on the
  249. implementation choice of your system C compiler.
  250.  
  251. consp cons car cdr set-car! set-cdr!
  252.  
  253. number? + - * / < > eqv?
  254. The arithmetic functions all take two arguments.
  255.  
  256. eq?, pointer objective identity. (Use eqv? for numbers.)
  257.  
  258. symbolconc, takes symbols as arguments and appends them. 
  259.  
  260. symbol?
  261.  
  262. symbol-bound? takes an optional environment structure.
  263. symbol-value also takes optional env.
  264. set-symbol-value also takes optional env.
  265.  
  266. env-lookup takes a symbol and an environment structure. If it returns
  267. non-nil the CAR will be the value of the symbol.
  268.  
  269. assq
  270.  
  271. read,print
  272.  
  273. eval, takes a second argument, an environment.
  274.  
  275. copy-list. Copies the top level conses in a list.
  276.  
  277. oblist, returns a copy of the list of the symbols that have been interned.
  278.  
  279. gc-status, prints out the status of garbage collection services, the
  280. number of cells allocated and the number of cells free. If given
  281. a () argument turns gc services off, if non-() turns gc services on.
  282. In mark-and-sweep storage management mode the argument only turns on
  283. and off verbosity of GC messages.
  284.  
  285. gc, does a mark-and-sweep garbage collection. If called with argument nil
  286. does not print gc messages during the gc.
  287.  
  288. load, given a filename (which must be a symbol, there are no strings)
  289. will read/eval all the forms in that file. An optional second argument,
  290. if T causes returning of the forms in the file instead of evaluating them.
  291.  
  292. save-forms, given a filename and a list of forms, prints the forms to the
  293. file. 3rd argument is optional, 'a to open the file in append mode.
  294.  
  295. quit, will exit back to the operating system.
  296.  
  297. error, takes a symbol as its first argument, prints the pname of this
  298. as an error message. The second argument (optional) is an offensive
  299. object. The global variable errobj gets set to this object for later
  300. observation.
  301.  
  302. null?, not. are the same thing.
  303.  
  304. *catch tag exp, Sets up a dynamic catch frame using tag. Then evaluates exp.
  305.  
  306. *throw tag value, finds the nearest *catch with an EQ tag, and cause it to
  307. return value.
  308.  
  309. [Procedures in main program siod.c]
  310.  
  311. edit is a VMS specific function that takes a single filename argument
  312. and calls the EDT editor (as a procedure) to edit the file.
  313.  
  314. cfib is the same as standard-fib. You can time it and compare it with
  315. standard-fib to get an idea of the overhead of interpretation.
  316.  
  317. [Utility procedures in siod.scm:]
  318.  
  319. Shows how to define macros.
  320.  
  321. cadr,caddr,cdddr,replace,list.
  322.  
  323. (defvar variable default-value)
  324.  
  325. And for us old maclisp hackers, setq and defun, and progn, etc.
  326.  
  327. call-with-current-continuation
  328.  
  329. Implemented in terms of *catch and *throw. So upward continuations
  330. are not allowed.
  331.  
  332. A simple backquote (quasi-quote) implementation.
  333.  
  334. cond, a macro.
  335.  
  336. append
  337.  
  338. nconc
  339.  
  340. [A streams implementation:]
  341.  
  342. The first thing we must do is decide how to represent a stream.
  343. There is only one reasonable data structure available to us, the list.
  344. So we might use (<stream-car> <cache-flag> <cdr-cache> <cdr-procedure>)
  345.  
  346. the-empty-stream is just ().
  347.  
  348. empty-stream?
  349.  
  350. head
  351.  
  352. tail
  353.  
  354. cons-stream is a special form. Wraps a lambda around the second argument.
  355.  
  356. *cons-stream is the low-level constructor used by cons-stream.
  357.  
  358. [Benchmarks:]
  359.  
  360. A standard-fib procedure is included in siod.scm so that everyone will
  361. use the same definition in any reports of speed. Make sure the return
  362. result is correct. use command line argument of
  363.  %siod -h100000 -isiod.scm
  364.  
  365. (standard-fib 10) => 55 ; 795 cons work.
  366. (standard-fib 15) => 610 ; 8877 cons work.
  367. (standard-fib 20) => 6765 ; 98508 cons work.
  368.  
  369. [Porting:]
  370.  
  371. The only code under #ifdef is the definition of myruntime, which
  372. should be defined to return a double float, the number of cpu seconds
  373. used by the process so far. It uses the the tms_utime slot, and assumes
  374. a clock cycle of 1/60'th of a second.
  375.  
  376. The stack and register marking code used in the mark-and-sweep GC
  377. is unlikely to work on machines that do not keep the procedure call
  378. stack in main memory at all times. It is assumed that setjmp saves
  379. all registers into the jmp_buff data structure.
  380.  
  381. There is a bit of type casting in close_open_files and vload. The
  382. pname of an un-interned symbol is used as a pointer to FILE. This
  383. saves the code (a conser, a print case, and two gc cases) of defining
  384. a new data type for keeping track of binary data.
  385.  
  386. If the stack is not always aligned (in LISP-PTR sense) then the 
  387. gc_mark_and_sweep procedure will not work properly. 
  388.  
  389. Example, assuming a byte addressed 32-bit pointer machine:
  390.  
  391. stack_start_ptr: [LISP-PTR(4)] 
  392.                  [LISP-PTR(4)]
  393.                  [RANDOM(4)]
  394.                  [RANDOM(2)]
  395.                  [LISP-PTR(4)]
  396.                  [LISP-PTR(4)]
  397.                  [RANDOM(2)]
  398.                  [LISP-PTR(4)]
  399.                  [LISP-PTR(4)]
  400. stack_end:       [LISP-PTR(4)]
  401.  
  402. As mark_locations goes from start to end it will get off proper alignment
  403. somewhere in the middle, and therefore the stack marking operation will
  404. not properly identify some valid lisp pointers.
  405.  
  406. Fortunately there is an easy fix to this. A more aggressive use of
  407. our mark_locations procedure will suffice.
  408.  
  409. For example, say that there might be 2-byte random objects inserted into
  410. the stack. Then use two calls to mark_locations:
  411.  
  412.  mark_locations(((char *)stack_start_ptr) + 0,((char *)&stack_end) + 0);
  413.  mark_locations(((char *)stack_start_ptr) + 2,((char *)&stack_end) + 2);
  414.  
  415. If we think there might be 1-byte random objects, then 4 calls are required:
  416.  
  417.  mark_locations(((char *)stack_start_ptr) + 0,((char *)&stack_end) + 0);
  418.  mark_locations(((char *)stack_start_ptr) + 1,((char *)&stack_end) + 1);
  419.  mark_locations(((char *)stack_start_ptr) + 2,((char *)&stack_end) + 2);
  420.  mark_locations(((char *)stack_start_ptr) + 3,((char *)&stack_end) + 3);
  421.  
  422.  
  423. [Interface to other programs:]
  424.  
  425. If your main program does not want to actually have a read/eval/print
  426. loop, and instead wants to do something else entirely, then use
  427. the routine set_repl_hooks to set up for procedures for:
  428.  
  429.  * putting the prompt "> " and other info strings to standard output.
  430.  
  431.  * reading (getting) an expression
  432.  
  433.  * evaluating an expression
  434.  
  435.  * printing an expression.
  436.  
  437. The routine get_eof_val may be called inside your reading procedure
  438. to return a value that will cause exit from the read/eval/print loop.
  439.  
  440. In order to call a single C function in the context of the repl loop,
  441. you can do the following:
  442.  
  443. int flag = 0;
  444.  
  445. void my_puts(st)
  446.  char *st;
  447. {}
  448.  
  449. LISP my_reader()
  450. {if (flag == 1)
  451.   return(get_eof_val());
  452.  flag == 1;
  453.  return(NIL);}
  454.  
  455. LISP my_eval(x)
  456.  LISP x;
  457. {call_my_c_function();
  458.  return(NIL);}
  459.  
  460. LISP my_print(x)
  461.  LISP x;
  462. {}
  463.  
  464. do_my_c_function()
  465. {set_repl_hooks(my_puts,my_read,my_eval,my_print);
  466.  repl_driver(1, /* or 0 if we do not want lisp's SIGINT handler */
  467.              0);}
  468.  
  469.  
  470. If you need a completely different read-eval-print-loop, for example
  471. one based in X-Window procedures such as XtAddInput, then you may want to
  472. have your own input-scanner and utilize a read-from-string kind of
  473. function.
  474.  
  475.  
  476. [User Type Extension:]
  477.  
  478. There are 5 user types currently available. tc_user_1 through tc_user_5.
  479. If you use them then you must at least tell the garbage collector about
  480. them. To do this you must have 4 functions,
  481.  * a user_relocate, takes a object and returns a new copy.
  482.  * a user_scan, takes an object and calls relocate on its subparts.
  483.  * a user_mark, takes an object and calls gc_mark on its subparts or
  484.                 it may return one of these to avoid stack growth.
  485.  * a user_free, takes an object to hack before it gets onto the freelist.
  486.  
  487. set_gc_hooks(user_relocate_fcn,
  488.              user_scan_fcn,
  489.              user_mark_fcn,
  490.              user_free_fcn,
  491.              &kind_of_gc);
  492.  
  493. kind_of_gc should be a long. It will receive 0 for mark-and-sweep, 1 for
  494. stop-and-copy. Therefore set_gc_hooks should be called AFTER process_cla.
  495. You must specify a relocate function with stop-and-copy. The scan
  496. function may be NULL if your user types will not have lisp objects in them.
  497. Under mark-and-sweep the mark function is required but the free function
  498. may be NULL.
  499.  
  500. See SIOD.C for a very simple string-append implementation example.
  501.  
  502. You might also want to extend the printer. This is optional.
  503.  
  504. set_print_hooks(fcn);
  505.  
  506. The fcn receives the object which should be printed to its second
  507. argument, a FILE*.
  508.  
  509. The evaluator may also be extended, with the "application" of user defined
  510. types following in the manner of an MSUBR.
  511.  
  512. Lastly there is a simple read macro facility.
  513.  
  514. set_read_hooks("All-Readmacros",
  515.                "Token-Ending-Readmacros",
  516.                 read_macro_handling_fcn);
  517.  
  518. The read_macro_handling_fcn will receive the character used to trigger
  519. it and the struct gen_readio * being read from. It should return a lisp object.
  520.  
  521.  
  522.